package com.itheima.stock.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.stock.map.SysUserToPageMapper;
import com.itheima.stock.mapper.SysUserMapper;
import com.itheima.stock.pojo.SysUser;
import com.itheima.stock.service.SysUserService;
import com.itheima.stock.vo.req.LoginReqVo;
import com.itheima.stock.vo.req.UsersPageReqVo;
import com.itheima.stock.vo.resp.LoginRespVo;
import com.itheima.stock.vo.resp.PageVo;
import com.itheima.stock.vo.resp.R;
import com.itheima.stock.vo.resp.UsersPageRespVo;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @ClassName SysUserServiceImpl
 * @Description TODO
 * @date 2023/2/23 15:41
 * @Version 1.0
 */
@Service
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    SysUserMapper sysUserMapper;
    @Autowired
    StringRedisTemplate redis;
    @Autowired
    PasswordEncoder passwordEncoder;

    /**
     * 用户分页方法
     * @param vo
     * @return
     */
    @Override
    public PageVo<UsersPageRespVo> getUserPage(UsersPageReqVo vo) {
        System.out.println(vo);

//        List<SysUser> sysUsers = sysUserMapper.usersPage(vo);
//        System.out.println("结果："+sysUsers);
        Integer pageNum = (vo.getPageNum()-1)*vo.getPageSize();
        Integer pageSize = vo.getPageSize();
        PageHelper.startPage(pageNum,pageSize);

        Page<SysUser> sysUsers = (Page<SysUser>) sysUserMapper.selectToUser(vo);
        System.out.println(sysUsers);
        List<SysUser> result = sysUsers.getResult();
        ArrayList<UsersPageRespVo> usersPageRespVos = new ArrayList<UsersPageRespVo>();
        for (SysUser sysUser : result) {
            UsersPageRespVo conver = SysUserToPageMapper.INSTANCT.conver(sysUser);
            String createUserName = sysUserMapper.selectCreateNameById(sysUser.getCreateId());
            String updateUserName = sysUserMapper.selectUpdateNameById(sysUser.getUpdateId());
            conver.setCreateUserName(createUserName);
            conver.setUpdateUserName(updateUserName);
            usersPageRespVos.add(conver);
        }
        PageVo<UsersPageRespVo> usersPageRespVoPageVo = new PageVo<>();
        usersPageRespVoPageVo.setRows(usersPageRespVos);
        usersPageRespVoPageVo.setSize(sysUsers.getPageSize());
        //当前界面
        usersPageRespVoPageVo.setCurrentPage(sysUsers.getPages());
        //总行数
        usersPageRespVoPageVo.setTotalRows(Integer.parseInt(String.valueOf(sysUsers.getTotal())));
        return usersPageRespVoPageVo;
    }


    /**
     * 登录实现方法
     * @param vo
     * @return
     */
    @Override
    public R<LoginRespVo> login(LoginReqVo vo) {
        //校验接收参数
        if (Objects.isNull(vo.getPassword())||Objects.isNull(vo.getUsername())||Objects.isNull(vo.getCode())){
            return R.error("登录失败");
        }
        //根据参数在redis中取值
        String s = redis.opsForValue().get(vo.getRkey());
        //校验验证码
        if (!vo.getCode().equals(s)){
            return R.error("验证码错误");
        }
        //根据用户名查询用户
        SysUser sysUser = sysUserMapper.selectOneToUserName(vo.getUsername());
        if (Objects.isNull(sysUser)){
        return R.error("登录失败请检查账户密码");
        }
        //校验密码是否正确
        boolean matches = passwordEncoder.matches(vo.getPassword(), sysUser.getPassword());
        if (matches){
            //构造结果集，返回结果
            LoginRespVo loginRespVo = new LoginRespVo(sysUser.getId(), sysUser.getPhone(), sysUser.getUsername(), sysUser.getNickName());
            return R.ok("登录成功",loginRespVo);
        }
        return R.error("登录失败");
    }

    /**
     * 测试方法
     * @return
     */
    @Override
    public List<SysUser> getAllSysUser() {

        return null;
    }

}
